﻿/**
* @file MAX6675.c
* @brief this is a brief description
* @details this is the detail description.
* @author fulong 
* @data 2015/1/7
* @version 1.0
* @par Copyright (c):
* @par First build
*     version: fulong 
*     data: 2015/1/7
*/
#include "MAX6675.h"
#include "../bsp/bsp.h"
#include "../lib/delay.h"
#include "io_configure.h"
#ifdef __STM32
    #include "../bsp/arch/stm32/stm32f1xx/STM32F10x_StdPeriph_Driver/inc/stm32f10x_gpio.h"
#endif
#ifdef TEMP_MAX6675_FILTER
/*!< K型热电偶0-109度1刻度的分度表 */
const uint16 K_table[K_TABLE_LEN] = {
    0, 39, 79, 119, 158, 198, 238, 277, 317, 357,
    397, 437, 477, 517, 557, 597, 637, 677, 718, 758,
    798, 838, 879, 919, 960, 1000, 1041, 1081, 1122, 1162,
    1203, 1244, 1285, 1325, 1366, 1407, 1448, 1489, 1529, 1570,
    1611, 1652, 1693, 1734, 1776, 1817, 1858, 1899, 1940, 1981,
    2022, 2064, 2105, 2146, 2188, 2229, 2270, 2312, 2353, 2394,
    2436, 2477, 2519, 2560, 2601, 2643, 2684, 2726, 2767, 2809,
    2850, 2892, 2933, 2875, 3016, 3058, 3100, 3141, 3183, 3224,
    3266, 3307, 3349, 3390, 3421, 3473, 3515, 3556, 3598, 3639,
    3681, 3722, 3764, 3805, 3847, 3888, 3930, 3971, 4012, 4054,
    4095, 4137, 4178, 4219, 4261, 4302, 4343, 4384, 4426, 4467
};
const uint16 K_table_index[K_TABLE_INDEX_LEN] = {
    0, 11, 21, 30, 40, 50, 60, 69, 79, 89, 98, 108
};
static uint8 max6675_first_select = 0;
    #define MAX6675_SELECT() max6675_cs_off()
    #define MAX6675_UNSELECT() max6675_cs_on()
    #define MAX6675_CLK_HIGH() max6675_clk_on()
    #define MAX6675_CLK_LOW() max6675_clk_off()
void max6675_init(void)
{
    SET_CS_OUTPUT();
    SET_CLK_OUTPUT();
    SET_SO_INPUT();
    MAX6675_UNSELECT();
    MAX6675_CLK_HIGH();
}
uint16  get_max6675_data(void)
{
    uint16 max6675_sample = 0;
    uint8 sample_counter;
    if (max6675_first_select == 0)
    {
        MAX6675_SELECT();
        max6675_first_select = 1;
    }
    if (get_so_data() == 0)
    {
        for (sample_counter = 0; sample_counter < 16; sample_counter++)
        {
            MAX6675_CLK_LOW();
            max6675_sample |= get_so_data();
            MAX6675_CLK_HIGH();
            if (sample_counter < 15)
            {
                max6675_sample  <<= 1;
            }
        }
        MAX6675_UNSELECT();
        max6675_first_select = 0;
    }
    return (max6675_sample);
}
#endif
